﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using System.IO;
using Bussiness;
using WebControls.GaleriaImagenes;
using System.Collections.Generic;
using System.Configuration;

namespace Presentation.Controls
{
    public partial class ajaxfileupload : System.Web.UI.UserControl
    {
        string savePath;
        string ruta;
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void AsyncFileUpload_OnUploadedComplete(object sender, AsyncFileUploadEventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "size", "top.$get(\"" + uploadResult.ClientID + "\").innerHTML = 'Uploaded size: " + AsyncFileUpload.FileBytes.Length.ToString() + "';", true);
            // Uncomment to save to AsyncFileUpload\Uploads folder.
            // ASP.NET must have the necessary permissions to write to the file system.

            savePath = MapPath(UploadPath + Path.GetFileName(e.filename));
            ruta = Path.GetFileName(AsyncFileUpload.FileName);
            if (ControladoraABMImagen.checkFileNameExist(ruta))
            {
                AsyncFileUpload_OnUploadedFileError(this, new AsyncFileUploadEventArgs(AsyncFileUploadState.Failed, "Ya existe un archivo con ese nombre, modifiquelo", e.filename, e.filesize));
                return;
            }            
            AsyncFileUpload.SaveAs(savePath);
            AsyncFileUpload.Enabled = false;
        }

        protected void AsyncFileUpload_OnUploadedFileError(object sender, AsyncFileUploadEventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "top.$get(\"" + uploadResult.ClientID + "\").innerHTML = 'Error: " + e.statusMessage + "';", true);
        }

        private string _UploadPath;
        public string UploadPath { get { return (_UploadPath); } set { _UploadPath = value; } }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {                        
            //((List<IImagen>)Session["lisImagenesTemp"]).Add(ControladoraImagen.agregarImagen(txtAlt.Text, savePath, txtTitulo.Text));
            ControladoraABMImagen.agregarImagen(txtAlt.Text, ruta, txtTitulo.Text);
        }
    }
}
